<?php
/**
 * Created by PhpStorm.
 * User: yanyan
 * Date: 2018/1/2
 * Time: 11:24
 */

namespace wap\Controller;


class JollyController extends ApibaseController
{
    /*
     * Jollyone商品列表
     */
    public function Jolly_list(){
        $where = ['delete_at'=>0];
        if(I('post.jolly_id')){
            $where = ['jolly_id'=>I('post.jolly_id')];
        }
        $JollyData = M('JollyEntity')->where($where)->order('jolly_id desc')->select();
        if ($JollyData){
            dataFormat($JollyData,1,'success');
        }else{
            dataFormat('',0,'暂无数据');
        }
    }
    /*
     * Jollyone商品下单
     * 传参 商品id 数量 购买人 手机号 详细地址
     */
    public function make_order(){
        if(I('post.is_buy') && I('post.jolly_id') && I('post.num') && I('post.user_name') && I('post.user_mobile') && I('post.receive_address')){
            $jollyone = M('JollyEntity')->where(['jolly_id'=>I('post.jolly_id')])->find();
            $total_price = round($jollyone['jolly_price'] * I('post.num') , 2);
            $user = M('UserInfo')->where(['user_id'=>session('loginUid')])->find();
            if($user['ordinary_score'] < $total_price){
                dataFormat('',0,'账户积分不足');
            }
            $JollyOrder  = M('JollyOrder');
            $UserInfo    = M('UserInfo');
            $ScoreDetail = M('ScoreDetail');
            $JollyOrder  ->startTrans();
            $UserInfo    ->startTrans();
            $ScoreDetail ->startTrans();

            $order = $JollyOrder->lock(true)->add([
                'order_id'        => 'Jolly'.mkorder(),
                'user_id'         => session('loginUid'),
                'user_name'       => I('post.user_name'),
                'user_mobile'     => I('post.user_mobile'),
                'jolly_id'        => I('post.jolly_id'),
                'num'             => I('post.num'),
                'buyer_msg'       => I('post.buyer_msg'),
                'jolly_price'     => $jollyone['jolly_price'],
                'total_price'     => $total_price,
                'receive_address' => I('post.receive_address'),
                'add_at'          => time(),
                'update_at'       => time(),
            ]);
            if($order){
                //生成购买订单，对应账户减掉商品总价，新增账户的积分使用明细
                $rs1 = $UserInfo->lock(true)->where(['user_id'=>session('loginUid')])->setDec('ordinary_score',$total_price);
                $rs2 = $ScoreDetail->lock(true)->add([
                    'user_id'      => session('loginUid'),
                    'detail_score' => $total_price,
                    'detail_type'  => 2, //消费使用
                    'score_type'   => 1,  //普通积分
                    'remark'       => '购买Jolly商品：'.$jollyone['jolly_name'],
                    'add_at'       => time(),
                    'update_at'    => time()
                ]);
                if ($rs1 && $rs2){
                    $JollyOrder->commit();
                    $UserInfo->commit();
                    $ScoreDetail->commit();
                    dataFormat('',1,'下单成功');
                }else{
                    $JollyOrder->rollback();
                    $UserInfo->rollback();
                    $ScoreDetail->rollback();
                    dataFormat('',0,'商品订单生成失败');
                }
            }else{
                $JollyOrder->rollback();
                dataFormat('',0,'订单生成失败');
            }
        }else{
            dataFormat('',0,'商品订单参数缺失');
        }
    }
    /*
     * jolly商品订单列表
     * 参数：订单发货状态（order_status 默认0待发货）
    */
    public function order_list(){
        $order_data = M('JollyOrder')->where(['user_id'=>session('loginUid')])->order('id desc')->select();
        if($order_data){foreach ($order_data as $k=>$val){
            $goods = M('JollyEntity')->where(['jolly_id'=>$val['jolly_id']])->find();
            $order_data[$k]['jolly_name'] = $goods['jolly_name'];
            $order_data[$k]['jolly_face_img'] = $goods['jolly_face_img'];
        }}
        if($order_data){
            dataFormat($order_data,1,'success');
        }else{
            dataFormat('',0,'error');
        }
    }
    /*
     * jolly商品取消订单
     * 参数：order_id
    */
    public function jolly_cancel(){
        $order_id = I('post.order_id');
        if(!$order_id){
            dataFormat('',0,'参数缺失');
        }
        $order = M('JollyOrder')->where(['order_id'=>$order_id])->find();
        if ($order['send_at']) {
            dataFormat('', 0, '已发货的商品订单不能取消');
        }
        $order_update = M('JollyOrder')->where(['order_id'=>$order_id])->setField([
            'order_status' => '-1',
            'update_at'    => time()
        ]);
        if ($order_update){
            $score_return = M('UserInfo')->where(['user_id'=>session('loginUid')])->setInc('ordinary_score',$order['total_price']);
            //取消成功 积分返回账户 生成对应的积分明细
            $detail_add = M('ScoreDetail')->add([
                'user_id'      => session('loginUid'),
                'detail_type'  => 3, //取消订单 积分返回+
                'score_type'   => 1, //普通账户积分
                'detail_score' => $order['total_price'],
                'remark'       => '取消订单'.$order_id,
                'add_at'       => time(),
                'update_at'    => time(),

            ]);
            if($score_return && $detail_add){
                dataFormat('',1,'success');
            }
        }else{
            dataFormat('',0,'error');
        }
    }

    /*
     * jolly商品订单手动确认收货
     * 发货时间send_at 七天后自动确认收货
    */
    public function confirm_order(){
        $order_id = I('post.order_id');
        $order = M('JollyOrder')->where(['order_id'=>$order_id,'order_status'=>1])->find();
        if($order){
            $rs = M('JollyOrder')->where(['order_id'=>$order_id,'order_status'=>1])->setField([
                'order_status' => 2,
                'complete_at'  => time(),
            ]);
            if ($rs){
                dataFormat('',1,'success');
            }
        }else{
            dataFormat('',0,'error');
        }
    }
    /*
     * jolly商品订单详情
    */
    public function order_detail(){
        $order_id = I('post.order_id');
        if($order_id){
            $orderDetail = M('JollyOrder o')->join('pf_jolly_entity j on o.jolly_id=j.jolly_id')->where(['order_id'=>$order_id])->find();
            $order_at = M('JollyOrder')->where(['order_id'=>$order_id])->getField('add_at');
            $orderDetail['add_at'] = date('Y-m-d H:i:s',$order_at);
            dataFormat($orderDetail,1,'success');
        }else{
            dataFormat('',0,'参数缺失');
        }
    }

    /*
     * jolly商品订单七天自动收货
     * 发货时间send_at 七天后自动确认收货
    */
    public function auto_confirm_order(){
        $confirm_order = M('JollyOrder')->where(['send_at'=>['lt',time()-7*24*3600],'order_status'=>1])->select();
        if($confirm_order){
            M('JollyOrder')->where(['send_at'=>['lt',time()-7*24*3600],'order_status'=>1])->setField([
                'order_status'     => 2,
                'is_auto_complete' => 1,
                'complete_at'      => time(),
            ]);
        }
    }
}